package ch.abcm.remsmobile.REMSMobile.externnotifier;

import ch.abcm.remsmobile.REMSMobile.R;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

public class ExternNotifierController extends BroadcastReceiver {

	private static final String TAG = "ExternNotifierController";
	private static final String EXTERN_NOTIFIER_ACTION = "ch.bfh.abcm.rems.EXTERN_NOTIFIER_ACTION";
	private static final String EXTERN_NOTIFIER_TEST_ACTION = "ch.bfh.abcm.rems.EXTERN_NOTIFIER_TEST_ACTION";
	private static final String MESSAGE_EXTRA_TYPE = "MessageType";
	private static final String MESSAGE_EXTRA_TEXT = "MessageText";

	private enum NotifType {
		SMS, TOAST, LOG;
	}

	private enum MessageType {
		SIMPLE, PERIODICUPDATE, SYSTEAM_ERROR, MEASUREMNETDIVERGENCE, TEST_SMS, UNKNOWN;
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		String message = intent.getStringExtra(MESSAGE_EXTRA_TEXT);
		Log.i(TAG, "Received Action: " + action);

		if (action != null && action.equalsIgnoreCase(EXTERN_NOTIFIER_TEST_ACTION)) {
			Log.i(TAG, "Send a Test SMS");
			sendSMS(context, message);
		} else if (action != null && action.equalsIgnoreCase(EXTERN_NOTIFIER_ACTION)) {
			Log.i(TAG, "Notify the Recepient...");
			NotifType notifType = getNotifType(context);
			Log.d(TAG, "NotifType: " + notifType);
			Log.d(TAG, "NotificationMessage:"+message);
			switch (notifType) {
			case SMS:
				sendSMS(context, message);
				break;
			case TOAST:
				Log.i(TAG, message);
				Toast.makeText(context, message, Toast.LENGTH_LONG).show();
				break;
			case LOG:
				Log.i("NOTIF", message);
				break;
			}

		}
	}

	private NotifType getNotifType(Context context) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		String notificationTypeKey = context.getResources().getString(R.string.prefs_key_list_notification);
		Log.d(TAG, "NotificationType from Stringresources: " + notificationTypeKey);
		int pos = prefs.getInt(notificationTypeKey, 0);// 0 = Log
		return NotifType.values()[pos];
	}

	public static Intent getSimpleMessageIntent(String message) {
		Intent simpleMessageIntent = new Intent(EXTERN_NOTIFIER_ACTION);
		simpleMessageIntent.putExtra(MESSAGE_EXTRA_TYPE, MessageType.SIMPLE);
		simpleMessageIntent.putExtra(MESSAGE_EXTRA_TEXT, message);
		return simpleMessageIntent;
	}

	public static Intent getSendTestSMSIntent(String message) {
		Intent simpleMessageIntent = new Intent(EXTERN_NOTIFIER_TEST_ACTION);
		simpleMessageIntent.putExtra(MESSAGE_EXTRA_TYPE, MessageType.SIMPLE);
		simpleMessageIntent.putExtra(MESSAGE_EXTRA_TEXT, message);
		return simpleMessageIntent;
	}

	private void sendSMS(Context context, String message) {
		Toast.makeText(context,"Send SMS not implemented /n"+message, Toast.LENGTH_LONG).show();
		Log.e(TAG, "NOT IMPLEMENTED");
	}
}
